/**
 * 方法：时间格式化
 * @param {String} date - 要处理的时间（可以是new Date()获取的时间）
 * @param {String} format - 拼接的格式
 * @param {String} separator - 分隔符
 * @returns {String} - 处理后的时间格式
 *
 * 示例用法
 * 调用 FormatDate 方法
 * const result = FormatDate();
 * 输出结果
 * //console.log(result);
 * result = '2022-01-01 00:00:00'
 */
export function FormatDate(date:any, format:any, separator = '-') {
  if(!date) return '';
  
  const now = date ? new Date(date) : new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const day = String(now.getDate()).padStart(2, '0');
  const hours = String(now.getHours()).padStart(2, '0');
  const minutes = String(now.getMinutes()).padStart(2, '0');
  const seconds = String(now.getSeconds()).padStart(2, '0');

  // 若没有传入日期，则获取当前日期的年月日
  if (!date) {
    const currentDate = new Date();
    const currentYear = currentDate.getFullYear();
    const currentMonth = String(currentDate.getMonth() + 1).padStart(2, '0');
    const currentDay = String(currentDate.getDate()).padStart(2, '0');

    if (format === 'yyyy') {
      return `${currentYear}`;
    } else if (format === 'MM') {
      return `${currentMonth}`;
    } else if (format === 'yyyy-MM') {
      return `${currentYear}${separator}${currentMonth}`;
    } else if (format === 'yyyy-MM-dd') {
      return `${currentYear}${separator}${currentMonth}${separator}${currentDay}`;
    }
  }

  // 根据传入的 format 参数格式化日期时间
  if (format === 'yyyy') {
    return `${year}`;
  } else if (format === 'MM') {
    return `${month}`;
  } else if (format === 'yyyy-MM') {
    return `${year}${separator}${month}`;
  } else if (format === 'yyyy-MM-dd') {
    return `${year}${separator}${month}${separator}${day}`;
  } else if (format === 'yyyy-MM-dd-HH') {
    return `${year}${separator}${month}${separator}${day}-${hours}`;
  } else if (format === 'yyyy-MM-dd-HH-mm') {
    return `${year}${separator}${month}${separator}${day}-${hours}${separator}${minutes}`;
  } else if (format === 'yyyy-MM-dd-HH-mm-ss') {
    return `${year}${separator}${month}${separator}${day}-${hours}${separator}${minutes}${separator}${seconds}`;
  } else {
    // 默认返回标准格式
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }
}